﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Threading.Thread;
using static System.Console;

namespace UnitTest
{
    public class Unit_01
    {
        /*
         * 编写程序实现,子线程循环3次,接着主线程循环5次,接着再子线程循环3次,主线程循环5次,
         * 如此反复,循环3次
         */
        public static void Go()
        {
            Print(1);
            for (int i = 1; i < 4; i++)
            {
            }
        }

        private static object lock1=new object();
        private static void Print(int n)
        {
            WriteLine($"第{n}次运行");
            Task task = Task.Run(() =>
            {
                Task.Delay(100);
                for (int i = 1; i < 4; i++)
                {
                    WriteLine($"子线程循环第{i}次");
                    Common.WriteCurrentTaskInfo($"子线程");
                }
            });
            for (int i = 1; i < 6; i++)
            {
                WriteLine($"主线程循环第{i}次");
                Common.WriteCurrentTaskInfo($"主线程");
            }

            task.ContinueWith(t =>
            {
                for (int i = 1; i < 4; i++)
                {
                    WriteLine($"子线程循环第{i + 3}次");
                    Common.WriteCurrentTaskInfo($"子线程");
                }
            },TaskContinuationOptions.AttachedToParent);
            for (int i = 1; i < 6; i++)
            {
                WriteLine($"主线程循环第{i + 5}次");
                Common.WriteCurrentTaskInfo($"主线程");
            }
        }
    }
}
